package com.example.text.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.common.result.Result;
import com.example.text.common.result.ResultFactory;
import com.example.text.entity.*;
import com.example.text.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class RoleController {



    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    AdminPermissionService adminPermissionService;

    @Autowired
    AdminRolePermissionService adminRolePermissionService;

    @CrossOrigin
    @GetMapping("/api/admin/role/{size}/{page}")
    public Result listRoles(@PathVariable("size") int size, @PathVariable("page") int page) throws Exception {
        System.out.println("一页有："+size+"个数据");
        System.out.println("从第"+page+"个页开始");
        Page<AdminRole> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(adminRoleService.selectAdminRolePage(pages));
    }

    @GetMapping("/api/admin/role")
    public Result listRole() throws Exception {
        return ResultFactory.buildSuccessResult(adminRoleService.selectAll());
    }

    @CrossOrigin
    @PutMapping("/api/admin/role/status")
    public Result updateRoleStatus(@RequestBody AdminRole requestRole) {
        AdminRole adminRole = adminRoleService.updateRoleStatus(requestRole);
        String message = "用户" + adminRole.getName_zh()+ "状态更新成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @CrossOrigin
    @PutMapping("/api/admin/role")
    public Result editRole(@RequestBody AdminRole requestRole) {
        adminRoleService.addOrUpdate(requestRole);
        adminRolePermissionService.savePermChanges(requestRole.getId(), requestRole.getPerms());
        String message = "修改角色信息成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @CrossOrigin
    @PostMapping("/api/admin/role")
    public Result addRole(@RequestBody AdminRole requestRole) {
        adminRoleService.editRole(requestRole);
        return ResultFactory.buildSuccessResult("添加用户角色成功");
    }

    @CrossOrigin
    @GetMapping("/api/admin/role/perm")
    public Result listPerms() {
        List<AdminPermission> a=adminPermissionService.list();
        System.out.println("a:"+a);
        return ResultFactory.buildSuccessResult(a);
    }

    @CrossOrigin
    @PutMapping("/api/admin/role/menu")
    public Result updateRoleMenu(@RequestParam int rid, @RequestBody Map<String, List<Integer>> menusIds) {
        adminRoleMenuService.updateRoleMenu(rid, menusIds);
        return ResultFactory.buildSuccessResult("更新成功");
    }

    @PostMapping("/api/admin/role/delete")
    public  Result deleteRule(@RequestBody AdminRole requestRole) throws Exception {

        if(requestRole.getName().equals("sysAdmin")){
            String message = "管理者角色不可以删除";
            System.out.println(message);
            return ResultFactory.buildFailResult(message);
        }
           adminRoleService.deleteRole(requestRole);
        return ResultFactory.buildSuccessResult("角色删除成功");

    }
    @PostMapping("/api/admin/role/deletes/{ids}")
    public  Result deleteRules(@PathVariable("ids") String ids){
        String[] RoleIds = ids.split("\\,");
        for( String roleId: RoleIds ){
            if(roleId.equals("1")){
                String message="批量删除存在管理员角色，不可删除";

                return  ResultFactory.buildFailResult(message);
            }
        }
          adminRoleService.deleteRoles(RoleIds);
        return  ResultFactory.buildSuccessResult("批量删除成功");
    }

}




